package com.moxiao.yfjh.module.maker.common;

import com.moxiao.yfjh.framework.common.util.date.DateUtils;
import com.moxiao.yfjh.module.maker.controller.admin.makerProductInfo.vo.MatchmakerProductInfoCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchmakerInfo.vo.MatchmakerInfoCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.memberStringing.vo.MatchmakerMemberStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.dal.dataobject.makerProductInfo.MatchmakerProductInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchmakerInfo.MatchmakerInfoDO;
import com.moxiao.yfjh.module.maker.dal.dataobject.memberStringing.MatchmakerMemberStringingDO;
import com.moxiao.yfjh.module.maker.service.makerProductInfo.MatchmakerProductInfoService;
import com.moxiao.yfjh.module.maker.service.matchMakerStringing.MatchmakerStringingService;
import com.moxiao.yfjh.module.maker.service.matchmakerInfo.MatchmakerInfoService;
import com.moxiao.yfjh.module.maker.service.memberStringing.MatchmakerMemberStringingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * @version 1.0
 * @Author zhk
 * @Date 2024/1/11 9:48
 **/
@Service
public class GenMakerNoCommonUtil {

    /**
     * 红娘商品前缀
     */
    private static final String PREFIX_MATCH_MAKER_PRODUCT_INFO = "MP";
    /**
     * 红娘前缀
     */
    private static final String PREFIX_MATCH_MAKER_INFO = "MK";
    /**
     * 红娘牵线记录前缀
     */
    private static final String PREFIX_MATCH_STRINGING_INFO = "MS";
    /**
     * 会员购买红娘服务记录前缀
     */
    private static final String PREFIX_MATCH_MEMBER_STRINGING_INFO = "MU";
    @Resource
    private MatchmakerInfoService matchmakerInfoService;
    @Resource
    private MatchmakerProductInfoService matchmakerProductInfoService;
    @Resource
    private MatchmakerStringingService matchmakerStringingService;
    @Resource
    private MatchmakerMemberStringingService matchmakerMemberStringingService;

    /**
     * 增加 红娘商品no(红娘商品编号)字段
     *
     * @param createReqVO
     */
    public void addProductInfoNo(MatchmakerProductInfoCreateReqVO createReqVO) {
        MatchmakerProductInfoDO productInfoDO = matchmakerProductInfoService.getLastNo(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productInfoDO)) {
            String[] user_no = productInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MATCH_MAKER_PRODUCT_INFO + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PREFIX_MATCH_MAKER_PRODUCT_INFO + "_" + dateTime + "_" + "0001";
            Integer num = matchmakerProductInfoService.repeatChecking(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }

    /**
     * 增加 红娘no(红娘编号)字段
     *
     * @param createReqVO
     */
    public void addMatchmakerInfoNo(MatchmakerInfoCreateReqVO createReqVO) {
        MatchmakerInfoDO productInfoDO = matchmakerInfoService.getLastNo(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productInfoDO)) {
            String[] user_no = productInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MATCH_MAKER_INFO + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PREFIX_MATCH_MAKER_INFO + "_" + dateTime + "_" + "0001";
            Integer num = matchmakerInfoService.repeatChecking(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }

    /**
     * 增加 相亲红娘牵线记录no(相亲红娘牵线记录编号)字段
     *
     * @param createReqVO
     */
    public void addStringingNo(MatchmakerStringingCreateReqVO createReqVO) {
        MatchmakerStringingDO productInfoDO = matchmakerStringingService.getLastNo(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productInfoDO)) {
            String[] user_no = productInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MATCH_STRINGING_INFO + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PREFIX_MATCH_STRINGING_INFO + "_" + dateTime + "_" + "0001";
            Integer num = matchmakerStringingService.repeatChecking(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }


    /**
     * 增加 相亲红娘会员用户购买牵线no(相亲红娘牵线记录编号)字段
     *
     * @param createReqVO
     */
    public void addMemberStringingNo(MatchmakerMemberStringingCreateReqVO createReqVO) {
        MatchmakerMemberStringingDO productInfoDO = matchmakerMemberStringingService.getLastNo(DateUtils.getTodayStart(), DateUtils.getTodayEnd());
        DecimalFormat formatCount = new DecimalFormat("0000");
        int dateTime = Integer.parseInt(DateUtils.dateTime());
        if (Objects.nonNull(productInfoDO)) {
            String[] user_no = productInfoDO.getNo().split("_");
            String lastPart = user_no[user_no.length - 1];
            if (user_no.length > 1) {
                Integer num = Integer.parseInt(lastPart) + 1;
                String no = formatCount.format(num);
                createReqVO.setNo(PREFIX_MATCH_MEMBER_STRINGING_INFO + "_" + dateTime + "_" + no);
            }
        } else {
            String order_no = PREFIX_MATCH_MEMBER_STRINGING_INFO + "_" + dateTime + "_" + "0001";
            Integer num = matchmakerMemberStringingService.repeatChecking(order_no);
            if (num == 0) {
                createReqVO.setNo(order_no);
            }
        }
    }
}
